package org.ceroproject.ui.graphical;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

/**
 * This class creates the menu bar for the main frame of the GUI.
 * 
 * @author Camille Roux
 */
public class CeroMenuBar extends JMenuBar {
	
	/** serialVersionUID required for serializable interface */
	private static final long serialVersionUID = -7298672681855950607L;
	static CeroMenuBar menuBar;

	private CeroMenuBar() {
		setAlignmentX(0.0f);
		add(createFileMenu());
		add(createHelpMenu());
	}

	/**
	 * Build the File menu
	 * 
	 * @return the File menu
	 */
	private JMenu createFileMenu() {
		JMenu menu = new JMenu("File");

		JMenuItem newPlay = new JMenuItem("New Game");
		JMenuItem restart = new JMenuItem("Restart");
		JMenuItem exit = new JMenuItem("Exit");
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		menu.add(newPlay);
		menu.add(restart);
		menu.add(exit);

		return menu;
	}

	/**
	 * Build the Help menu
	 * 
	 * @return the Help menu
	 */
	private JMenu createHelpMenu() {
		JMenu menu = new JMenu("Help");

		JMenuItem help = new JMenuItem("Help");
		JMenuItem about = new JMenuItem("About Cero Project");
		menu.add(help);
		menu.add(about);
		about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0));

		return menu;
	}

	static public synchronized CeroMenuBar getInstance() {
		if (menuBar == null) {
			menuBar = new CeroMenuBar();
		}
		return menuBar;
	}

}
